﻿/******************************************************   
创建者: wdj
创建时间: 2017-12-28
功能: 小类细项占比设置--API
 ******************************************************/


using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.library;
using com.nm.library.Helper;
using george.Data;
using NHibernateVertica;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VerticaAPI.model;

namespace VerticaAPI
{
    [Description("小类细项占比设置--API")]
    public class DetailsPercentageSettingHandler : BaseHandler
    {
        public VerticaDataHelper Helper { get; set; } = new VerticaDataHelper();


        #region 查询列表API

        /// Writer：wdj
        /// Create Date：2017-12-28
        [MustLogin]
        [Description("查询")]
        [ReturnType(typeof(PaperData<DetailsPercentageSettingModel.ListModel>))]
        public ApiResult GetList(
            [ParamDescription("年")] int Year = 0,
            [ParamDescription("周")] int Week = 0,
            [ParamDescription("大类")] string Category = "",
            [ParamDescription("小类")] string Detail = "",
            [ParamDescription("性别")] string Gender = "",
            [ParamDescription("页码")] int Page = 1,
            [ParamDescription("页数")] int Size = 10)
        {
            try
            {
                if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
                {
                    return Error("该用户不属于任何公司!");
                }
                if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
                {
                    return Error("该用户所在公司没有任何数据源!");
                }
                var dt = ReturnList(Page, Size, Year, Week, Category, Detail, Gender);
                var count = dt.Rows.Count;
                var result = PaperData<DetailsPercentageSettingModel.ListModel>.Create(count, Size, Page);
                var list = ModelConvertHelper<DetailsPercentageSettingModel.ListModel>.ConvertToModel(dt);
                result.Items = list;
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion 查询列表API

        #region 获取列表

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="Size"></param>
        /// <param name="Year"></param>
        /// <param name="Week"></param>
        /// <param name="Category"></param>
        /// <param name="Detail"></param>
        /// <param name="Gender"></param>
        /// <returns></returns>
        public DataTable ReturnList(int Page, int Size, int Year, int Week, string Category, string Detail, string Gender)
        {
            try
            {
                string StrSql = "SELECT CompanyId, Extrsource, \"Year\", Week, Category, Gender, Detail, Persentage, StandardSKC, CreateTime, UpdateTime, Operator FROM ndw.ndw_details_percentage_setting where 1 = 1";
                StrSql += $" AND ExtrSource = '{UserInfo.UserExtrSource}' AND CompanyID= '{UserInfo.UserCompanyId}'";
                if (Year > 0)
                {
                    StrSql += " AND \"year\" =" + Year;
                }
                if (Week > 0)
                {
                    StrSql += " AND Week = " + Week;
                }
                if (!String.IsNullOrWhiteSpace(Category))
                {
                    StrSql += $" AND Category = '{Category}'";
                }
                if (!String.IsNullOrWhiteSpace(Detail))
                {
                    StrSql += $" AND Detail = '{Detail}'";
                }
                if (!String.IsNullOrWhiteSpace(Gender))
                {
                    StrSql += $" AND Gender = '{Gender}'";
                }

                //排序分页
                StrSql += $" Order By CompanyId, Extrsource, \"Year\", Week, Category, Gender, Detail, Persentage, StandardSKC, CreateTime, UpdateTime";
                StrSql += $" LIMIT {Size} offset {(Page - 1) * Size}";

                var cmd = Helper.GetSqlStringCommand(StrSql);
                return Helper.ExecuteDataTable(cmd);

            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion 获取列表


    }
}
